import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:wu_you/l10n/messages.dart';
///倒计时
class ItemOrderWidget extends StatefulWidget {
  final String time;
  final Color color;
  final dynamic message;
  ItemOrderWidget({Key key,this.time,this.color,this.message}) : super(key: key);
  @override
  _ItemOrderWidgetState createState() => _ItemOrderWidgetState();
}
class _ItemOrderWidgetState extends State<ItemOrderWidget>{
  String remainTime = '';
  Timer _timer;

  @override
  void initState() {
    super.initState();
    // 初始化开启倒计时
    startCountDown(widget.time);
  }

  @override
  void dispose() {
    super.dispose();
    // 在页面回收或滑动复用回收的时候把 timer 清除
    if (_timer != null) {
      if (_timer.isActive) {
        _timer.cancel();
        _timer = null;
      }
    }
  }

  @override
  void didUpdateWidget(covariant ItemOrderWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    // 外部重新请求接口后重新进行倒计时，这个方法是用来监控外部 setState 的
    startCountDown(widget.time);

  }

  void startCountDown(time){
    var nowTime = DateTime.now();
    var endTime = DateTime.parse(time.toString());
    // 如果剩余时间已经不足一分钟，则不必计时，直接标记超时
    if (endTime.millisecondsSinceEpoch - nowTime.millisecondsSinceEpoch < 1000 * 1) {
      setState(() {
        remainTime = '${widget.message['luck2']}';
      });
      return;
    }
    // 重新计时的时候要把之前的清除掉
    if (_timer != null) {
      if (_timer.isActive) {
        _timer.cancel();
        _timer = null;
      }
    }
    const repeatPeriod = const Duration(seconds: 1);
    caculateTime(nowTime, endTime);

    _timer = Timer.periodic(repeatPeriod, (timer) {
      //到时回调
      nowTime = nowTime.add(repeatPeriod);

      if (endTime.millisecondsSinceEpoch - nowTime.millisecondsSinceEpoch < 1000 * 1) {
        //取消定时器，避免无限回调
        timer.cancel();
        timer = null;

        setState(() {
          remainTime = '${widget.message['luck2']}';
        });

        return;
      }

      caculateTime(nowTime, endTime);
    });
  }

  /// 计算天数、小时、分钟、秒
  void caculateTime(nowTime, endTime) {
    var _surplus = endTime.difference(nowTime);
    int day = (_surplus.inSeconds ~/ 3600) ~/ 24;
    int hour = (_surplus.inSeconds ~/ 3600) % 24;
    int minute = _surplus.inSeconds % 3600 ~/ 60;
    int second = _surplus.inSeconds % 60;

    var str = '';
    int allHour = 0;
    if (day > 0) {
      // str = (day * 24).toString();
      allHour = day * 24;
    }
    if (hour > 0 || (day > 0 && hour == 0)) {
      if(hour / 10 < 1){
        str = str + '0' + (allHour + hour).toString() + "：";
      } else {
        str = (allHour + hour).toString() + "：";
      }
      // str = (allHour + hour).toString() + '：';
    }
    if (minute / 10 < 1) {//当只有个位数时，给前面加“0”，实现效果：“:01”,":02"
      str = str + '0' + minute.toString() + "：";
    } else {
      str = str + minute.toString() + "：";
    }

    if (second / 10 < 1) {
      str = str + '0' + second.toString();
    } else {
      str = str + second.toString();
    }

    // str = str + minute.toString() + '：' + second.toString();

    setState(() {
      remainTime = str;
    });
  }
  // var message;
  @override
  Widget build(BuildContext context) {
    // message = AppLocalizations.of(context);
    return (remainTime == "开奖已结束" || remainTime == "Draw Ended") ? Text("$remainTime",style: TextStyle(color: widget.color,fontSize: 16),) :
    Text("${widget.message['luck1']}：$remainTime",style: TextStyle(color: widget.color,fontSize: 16),);
  }
}